﻿using Newtonsoft.Json.Linq;
using SDF.Admin.Extensions;
using SDF.Admin.Infrastructure.Mapper;
using SDF.Core;
using SDF.Core.Infrastructure;
using SDF.Plugin.Misc.DynamicEntity.Common;
using SDF.Plugin.Misc.DynamicEntity.Domain;
using SDF.Plugin.Misc.DynamicEntity.Models;
using SDF.Services.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace SDF.Plugin.Misc.DynamicEntity.Extensions
{
    public static class MappingExtensions
    {
        //public static IDateTimeHelper _timeHelper = EngineContext.Current.Resolve<IDateTimeHelper>();

        #region common
        public static TDestination MapTo<TSource, TDestination>(this TSource source)
        {
            return AutoMapperConfiguration.Mapper.Map<TSource, TDestination>(source);
        }

        public static TDestination MapTo<TSource, TDestination>(this TSource source, TDestination destination)
        {
            return AutoMapperConfiguration.Mapper.Map(source, destination);
        }
        #endregion

        public static DynamicEntityHeaderModel ToModel(this DynamicEntityHeader entity)
        {
            //return entity.MapTo<DynamicEntityHeader, DynamicEntityHeaderModel>();

            var result = new DynamicEntityHeaderModel()
            {
                Id = entity.Id,
                CodeName = entity.CodeName,
                CreateTimeOnUtc = entity.CreateTimeOnUtc,//_timeHelper.ConvertToUserTime(entity.CreateTimeOnUtc),
                Delete = entity.Delete,
                Description = entity.Description,
                LimitedToProjects = entity.LimitedToProjects,
                Name = entity.Name,
                SAPInstance = entity.SAPInstance
            };

            //entity.Fields.ToList().ForEach(p =>
            //{
            //    result.Fields.Add(p.ToModel());
            //});

            return result;
        }

        public static DynamicEntityItemModel ToModel(this DynamicEntityItem item)
        {
            return new DynamicEntityItemModel()
            {
                Id = item.Id,
                CodeName = item.CodeName,
                CreateTimeOnUtc = item.CreateTimeOnUtc,//_timeHelper.ConvertToUserTime(item.CreateTimeOnUtc),
                DefaultValue = item.DefaultValue,
                Delete = item.Delete,
                FieldLength = item.FieldLength,
                FieldType = item.FieldType,
                Header = item.Header.ToModel(),
                HeaderId = item.Header.Id,
                Name = item.Name,
                ParamDirection = item.ParamDirection,
                ReferenceEntity = item.ReferenceEntity,
                SortNo = item.SortNo
            };
        }

        public static DynamicEntityItem ToEntity(this DynamicEntityItemModel model, DynamicEntityItem entity)
        {
            //return model.MapTo(destination);
            entity.HeaderId = model.HeaderId;
            entity.CodeName = model.CodeName;
            entity.DefaultValue = model.DefaultValue;
            entity.Delete = model.Delete;
            entity.FieldLength = model.FieldLength;
            entity.FieldType = model.FieldType;
            entity.Name = model.Name;
            entity.ParamDirection = model.ParamDirection;
            entity.ReferenceEntity = model.ReferenceEntity;
            entity.SortNo = model.SortNo;
            entity.CreateTimeOnUtc = model.CreateTimeOnUtc;

            return entity;
        }

        public static DynamicEntityHeader ToEntity(this DynamicEntityHeaderModel model, DynamicEntityHeader destination)
        {
            destination.Id = model.Id;
            destination.CodeName = model.CodeName;
            destination.Name = model.Name;
            destination.CreateTimeOnUtc = (model.CreateTimeOnUtc == DateTime.MinValue) ? DateTime.UtcNow : model.CreateTimeOnUtc;
            destination.LimitedToProjects = model.LimitedToProjects;
            destination.Delete = model.Delete;
            destination.Description = model.Description;
            destination.SAPInstance = model.SAPInstance;
            //destination.Fields = model.Fields;

            return destination;
        }

        public static DynamicEntityItem ToEntityItem(this JToken jToken, DateTime createTime, int index, DynamicEntityHeader referenceEntity)
        {
            return new DynamicEntityItem()
            {
                CodeName = jToken["ParamName"].Value<string>(),
                CreateTimeOnUtc = createTime,
                DefaultValue = jToken["DefaultValue"].Value<string>(),
                FieldLength = jToken["ParamLength"].Value<int>(),
                FieldType = Tools.ConvertTypeFromString(jToken["ParamType"].Value<string>()),
                Name = jToken["ParamDesc"].Value<string>().IsNullOrEmpty() ? jToken["ParamName"].Value<string>() : jToken["ParamDesc"].Value<string>(),
                ParamDirection = Tools.ConvertDirectionFromString(jToken["Direction"].Value<string>()),
                ReferenceEntity = referenceEntity,//todo
                SortNo = index++
            };
        }
    }
}